package com.gxuwz.medical.domain.person;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gxuwz.medical.database.DBUtil;
import com.gxuwz.medical.domain.family.Family;

/**
 * 农民档案信息模块类
 * @author 沉羽墨玖
 *
 */
public class Person {
	private String persCode;
	private Family family;
	private String ruralCard;
	private String relation;
	private String cardId;
	private String persName;
	private int age;
	private String sex;
	private String birthday;
	private String persPro;
	private boolean isRural;
	private String occupation;
	private String workUnit;
	private String liveAddress;
	private String telephone;
	
	private String famCode;

	public Person() {

	}

	public Person(String persCode) throws SQLException {
		this.persCode = persCode;
		this.load();
	}

	public Person(String persCode, String persName, String sex,
			String birthday, String persPro, String occupation,
			String workUnit, String liveAddress, String telephone) {
		super();
		this.persCode = persCode;
		this.persName = persName;
		this.sex = sex;
		this.birthday = birthday;
		this.persPro = persPro;
		this.occupation = occupation;
		this.workUnit = workUnit;
		this.liveAddress = liveAddress;
		this.telephone = telephone;

	}

	public Person(String persCode, Family family, String ruralCard,
			String relation, String cardId, String persName, int age,
			String sex, String birthday, String persPro, boolean isRural,
			String occupation, String workUnit, String liveAddress,
			String telephone) {
		super();
		this.persCode = persCode;
		this.family = family;
		this.ruralCard = ruralCard;
		this.relation = relation;
		this.cardId = cardId;
		this.persName = persName;
		this.age = age;
		this.sex = sex;
		this.birthday = birthday;
		this.persPro = persPro;
		this.isRural = isRural;
		this.occupation = occupation;
		this.workUnit = workUnit;
		this.liveAddress = liveAddress;
		this.telephone = telephone;
	}

	private void load() throws SQLException {
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DBUtil.getConn();
			String sql = "select * from person where persCode=?";
			pstmt = conn.prepareStatement(sql);
			int index = 1;
			pstmt.setString(index, this.persCode);
			rs = pstmt.executeQuery();
			rs.next();
			this.persCode = rs.getString("persCode");
			this.ruralCard = rs.getString("ruralCard");
			this.relation = rs.getString("relation");
			this.cardId = rs.getString("cardId");
			this.persName = rs.getString("persName");
			this.age = rs.getInt("age");
			this.sex = rs.getString("sex");
			this.birthday = rs.getString("birthday");
			this.persPro = rs.getString("persPro");
			this.isRural = rs.getBoolean("isRural");
			this.occupation = rs.getString("occupation");
			this.workUnit = rs.getString("workUnit");
			this.liveAddress = rs.getString("liveAddress");
			this.telephone = rs.getString("telephone");
			
			this.famCode=rs.getString("famCode");			

		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
	}

	public void addPerson(Connection conn) throws SQLException {
		try {
			// 生成个人编号
			this.persCode = createPersCode();

			saveToDB(conn);
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		}
	}

	/**
	 * 添加家庭成员
	 * 
	 * @param conn
	 * @throws SQLException
	 */
	public void addMember(int agriNum) throws SQLException {
		Connection conn = null;
		try {
			conn = DBUtil.getConn();
			conn.setAutoCommit(false);
			addPerson(conn);
			// 更新家庭档案信息的家庭人口数和农业人口数
			family.countNumber(conn, agriNum);
			conn.commit();
		} catch (SQLException e) {
			e.printStackTrace();
			conn.rollback();
			throw e;
		} finally {
			DBUtil.close(conn);
		}
	}

	/**
	 * 持久化数据
	 * 
	 * @param conn
	 * @throws SQLException
	 */
	private void saveToDB(Connection conn) throws SQLException {

		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer("insert into person (");
			sqlBuff.append("persCode,famCode,ruralCard,cardId,persName,age,sex,birthday,persPro,isRural,occupation");
			sqlBuff.append(",workUnit,liveAddress,telephone,relation)");
			sqlBuff.append("values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			int index = 1;
			pstmt.setString(index++, this.persCode);
			pstmt.setString(index++, this.family.getFamCode());
			pstmt.setString(index++, this.ruralCard);
			pstmt.setString(index++, this.cardId);
			pstmt.setString(index++, this.persName);
			pstmt.setInt(index++, this.age);
			pstmt.setString(index++, this.sex);
			pstmt.setString(index++, this.birthday);
			pstmt.setString(index++, this.persPro);
			pstmt.setBoolean(index++, this.isRural);
			pstmt.setString(index++, this.occupation);
			pstmt.setString(index++, this.workUnit);
			pstmt.setString(index++, this.liveAddress);
			pstmt.setString(index++, this.telephone);
			pstmt.setString(index++, this.relation);
			int count = pstmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
			throw e;
		} finally {
			DBUtil.close(pstmt);
		}
	}

	/**
	 * 生成个人编号
	 * 
	 * @return
	 * @throws SQLException
	 */
	private String createPersCode() throws SQLException {

		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "select max(persCode) from person where persCode like'"
				+ this.family.getFamCode() + "%' ";

		try {

			conn = DBUtil.getConn();
			pstmt = conn.prepareStatement(sql);
			rs = pstmt.executeQuery();
			String maxcode = "";
			Integer number = 1;
			if (rs.next()) {
				maxcode = rs.getString(1);
			}
			if (maxcode != null) {
				int beginIndex = maxcode.length() - 2;
				String no = maxcode.substring(beginIndex);
				number = Integer.parseInt(no);
				++number;
				no = String.format("%02d", number);
				maxcode = this.family.getFamCode() + no;
			} else {
				String no = String.format("%02d", number);
				maxcode = this.family.getFamCode() + no;
			}
			return maxcode;

		} catch (SQLException e) {
			e.printStackTrace();
			throw new SQLException("参合编号失败:" + e.getMessage(), e);
		} finally {
			DBUtil.close(rs, pstmt, conn);
		}

	}

	public void edit() throws SQLException {
		Connection conn = null;
		try {
			conn = DBUtil.getConn();
			conn.setAutoCommit(false);
			// 鏇存柊鎱㈡�鐥呬俊鎭埌鏁版嵁搴�
			editToDB(conn);
			conn.commit();
		} catch (SQLException e) {
			conn.rollback();
			throw e;
		} finally {
			DBUtil.close(conn);
		}
	}

	private void editToDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer(
					"update person t set t.ruralCard=?,t.persName=?,t.sex=?,t.birthday=?,t.persPro=?,t.occupation=?,t.workUnit=?,t.liveAddress=?,t.telephone=? where t.persCode=?");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			pstmt.setString(1, this.ruralCard);
			pstmt.setString(2, this.persName);
			pstmt.setString(3, this.sex);
			pstmt.setString(4, this.birthday);
			pstmt.setString(5, this.persPro);
			pstmt.setString(6, this.occupation);
			pstmt.setString(7, this.workUnit);
			pstmt.setString(8, this.liveAddress);
			pstmt.setString(9, this.telephone);
			pstmt.setString(10, this.persCode);
			pstmt.executeUpdate();

		} catch (SQLException e) {
			throw new SQLException("Failed to update person  !", e);
		} finally {
			DBUtil.close(pstmt);
		}

	}

	public void del(String persCode) throws SQLException {
		Connection conn = null;
		this.persCode = persCode;
		try {
			conn = DBUtil.getConn();
			conn.setAutoCommit(false);
			deleteFromDB(conn);
			conn.commit();
		} catch (SQLException e) {
			conn.rollback();
			throw e;
		} finally {
			DBUtil.close(conn);
		}
	}

	private void deleteFromDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer(
					"delete from person where persCode=?");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			pstmt.setString(1, this.persCode);
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("Failed to delete record from table !", e);
		} finally {
			DBUtil.close(pstmt);
		}
	}

	public String getPersCode() {
		return persCode;
	}

	public void setPersCode(String persCode) {
		this.persCode = persCode;
	}

	public Family getFamily() {
		return family;
	}

	public void setFamily(Family family) {
		this.family = family;
	}

	public String getRuralCard() {
		return ruralCard;
	}

	public void setRuralCard(String ruralCard) {
		this.ruralCard = ruralCard;
	}

	public String getRelation() {
		return relation;
	}

	public void setRelation(String relation) {
		this.relation = relation;
	}

	public String getCardId() {
		return cardId;
	}

	public void setCardId(String cardId) {
		this.cardId = cardId;
	}

	public String getPersName() {
		return persName;
	}

	public void setPersName(String persName) {
		this.persName = persName;
	}

	public int getAge() {
		return age;
	}

	public void setAge(int age) {
		this.age = age;
	}

	public String getSex() {
		return sex;
	}

	public void setSex(String sex) {
		this.sex = sex;
	}

	public String getBirthday() {
		return birthday;
	}

	public void setBirthday(String birthday) {
		this.birthday = birthday;
	}

	public String getPersPro() {
		return persPro;
	}

	public void setPersPro(String persPro) {
		this.persPro = persPro;
	}

	public boolean isRural() {
		return isRural;
	}

	public void setRural(boolean isRural) {
		this.isRural = isRural;
	}

	public String getOccupation() {
		return occupation;
	}

	public void setOccupation(String occupation) {
		this.occupation = occupation;
	}

	public String getWorkUnit() {
		return workUnit;
	}

	public void setWorkUnit(String workUnit) {
		this.workUnit = workUnit;
	}

	public String getLiveAddress() {
		return liveAddress;
	}

	public void setLiveAddress(String liveAddress) {
		this.liveAddress = liveAddress;
	}

	public String getTelephone() {
		return telephone;
	}

	public void setTelephone(String telephone) {
		this.telephone = telephone;
	}

	public String getFamCode() {
		return famCode;
	}

	public void setFamCode(String famCode) {
		this.famCode = famCode;
	}

	

}
